<?php

App::uses('AppController', 'Controller');

/**
 * Paginas Controller
 *
 * @property Pagina $Pagina
 * @property PaginatorComponent $Paginator
 */
class PaginasController extends PaginasAppController {

    /**
     * __construct
     *
     * @var mixed
     * @var mixed
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->set('title', __d('paginas', 'Páginas'));
    }

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /* --------------------------------------     Public    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->viewClass = 'Php';
        $default = array(
            'Pagina' => array(
                'id' => '',
                'titulo' => '',
                'default' => '',
                'autor' => '',
                'chaves' => '',
                'descricao' => '',
                'status' => '',
                'css' => '',
                'js' => '',
                'css_file' => '',
                'js_file' => '',
                'created' => '',
                'updated' => '',
                'usuario_id' => '',
                'ordem' => '',
                'exibir_menu' => '',
                'router' => 'index'
            ),
            'Conteudo' => array(),
            'Usuario' => array(
                'password' => '',
                'id' => '',
                'username' => '',
                'email' => '',
                'status' => '',
                'role' => '',
                'created' => '',
                'modified' => ''
            )
        );
        $this->Pagina->recursive = 2;

        $options = array(
            'conditions' => array(
                'Pagina.status' => 1,
            )
        );

        if (isset($this->request->params['pass'][0]) AND ! empty($this->request->params['pass'][0])) {
            $options['conditions']['Pagina.router'] = $this->request->params['pass'][0];
        }
        $paginas = $this->Pagina->find('first', $options);
        $this->set('paginas', Hash::merge($default, $paginas));
        $this->set('title', $paginas['Pagina']['titulo']);
        $this->render('Paginas.'.$paginas['Pagina']['router']);
    }

    /* --------------------------------------     Admin    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Pagina->recursive = 0;
        $this->set('paginas', $this->Paginator->paginate());
        $this->set('title', 'Pagina - Consultar');
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        if (!$this->Pagina->exists($id)) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        $options = array('conditions' => array('Pagina.' . $this->Pagina->primaryKey => $id));
        $this->set('pagina', $this->Pagina->find('first', $options));
        $this->set('title', 'Pagina - Detalhes');
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Pagina->create();
            if ($this->Pagina->save($this->request->data)) {
                $this->Session->setFlash(__('The %s has been saved.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect(array('action' => 'edit', $this->Pagina->id));
            } else {
                $this->Session->setFlash(__('The %s could not be saved. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        }
        $this->set('title', 'Pagina - Cadastrar');
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Pagina->exists($id)) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Pagina->save($this->request->data)) {
                $this->Session->setFlash(__('The %s has been saved.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The %s could not be saved. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        } else {
            $options = array('conditions' => array('Pagina.' . $this->Pagina->primaryKey => $id));
            $this->request->data = $this->Pagina->find('first', $options);
        }
        $this->set('title', 'Pagina - Editar');
    }

    /**
     * admin_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->Pagina->id = $id;
        if (!$this->Pagina->exists()) {
            throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->request->params['controller'])));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Pagina->delete()) {
            $this->Session->setFlash(__('The %s has been deleted.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-success'
            ));
        } else {
            $this->Session->setFlash(__('The %s could not be deleted. Please, try again.', Inflector::humanize($this->request->params['controller'])), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'
            ));
        }
        return $this->redirect($this->referer());
    }

}
